//
//  SharePlatformView.m
//  ZsfMall
//
//  Created by zsf on 16/6/22.
//  Copyright © 2016年 zsf. All rights reserved.
//

#import "SharePlatformView.h"
#import "AppDelegate.h"

static NSInteger const buttonTag = 1000;
@interface SharePlatformView()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    NSArray *_imageArray;
    NSMutableArray *_btnArray;
}
@property (nonatomic,strong) UIVisualEffectView *backView;
@property (nonatomic,copy) ClickShareButtonBlock shareBlock;
@property (nonatomic,strong)UINavigationController  *navigation;


@property (nonatomic, strong)UIImagePickerController *imagePicker;

@end
@implementation SharePlatformView

- (instancetype)initWithFrame:(CGRect)frame ImageArray:(NSArray *)imageArray didShareButtonBlock:(ClickShareButtonBlock)saveBlock
{
    if (self = [super initWithFrame:frame]) {
        _imageArray = [NSArray arrayWithArray:imageArray];
        self.shareBlock = saveBlock;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.backView = [[UIVisualEffectView alloc]initWithEffect:blur];
    self.backView.frame = CGRectMake(0, 0, kScreenW, kScreenH);

    [self addSubview:self.backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [_backView addGestureRecognizer:tap];
    
    
    _btnArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 2; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor redColor];
        [btn setBackgroundImage:[UIImage imageNamed:_imageArray[i]] forState:UIControlStateNormal];
        btn.tag = buttonTag + i;
        btn.bounds = CGRectMake(0, 0, 48, 48);
        btn.layer.cornerRadius = btn.frame.size.width /2;
        btn.clipsToBounds = YES;
        btn.alpha = 0;
        [btn addTarget:self action:@selector(ShareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnArray addObject:btn];
        [self addSubview:btn];
        btn.center = self.center;
    }
}


- (void)show
{
    NSArray* windowsArray = [UIApplication sharedApplication].windows;
    UIWindow *win = windowsArray[0];
    [win addSubview:self];
    
    CGPoint center = self.center;
    CGFloat centerX = center.x;

    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,1.5,1.5);
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        for (NSInteger i = 0; i < _btnArray.count; i ++) {
            
            CGFloat y = 0;
            if (i == 0) {
                y = -60;
            }else {
                y = 60;
            }
        
            CGPoint btnCenter = CGPointMake(centerX+y, CGRectGetMaxY(self.frame) - 120);
            UIButton * btn = _btnArray[i];
            btn.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
            btn.alpha = 1;
            /**
             *  usingSpringWithDamping：0-1 数值越小，弹簧振动效果越明显
             *  initialSpringVelocity ：数值越大，一开始移动速度越快
             */
            [UIView animateWithDuration:0.5 delay:0.1 * i  usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
                btn.center = btnCenter;
                btn.transform = transform;
                btn.alpha = 1;
            } completion:^(BOOL finished) {
                
            }];
        }
    }];
}
- (void)hide    //点击空白区域隐藏
{
   [self viewHiddenAnimation:NO with:-1];
}


-(void)ShareButtonClick:(UIButton *)button{
    
    [self hide];
    
    if (button.tag == 1001 && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //设置拍照
        self.imagePicker = [[UIImagePickerController alloc]init];
        self.imagePicker.delegate = [UIApplication sharedApplication].keyWindow.rootViewController;
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.allowsEditing = YES;
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.imagePicker animated:YES completion:nil];
        
//#pragma -mark - 拍照后使用照片 在YSLTabBarController里
        
    }else {
    
        //设置调用本地相册
        self.imagePicker = [[UIImagePickerController alloc]init];
        self.imagePicker.delegate = self;
        
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        //模态出本地相册
         YSLPicViewController * picVC = [[YSLPicViewController alloc]init];

        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController: [[UINavigationController alloc] initWithRootViewController:picVC] animated:YES completion:nil];
        
    }
}



- (void)viewHiddenAnimation:(BOOL)isClickBtn with:(NSInteger)btntag
{
    CGPoint center = self.center;
    CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
    for (NSInteger i = 0; i < _btnArray.count; i ++) {
        UIButton * btn = _btnArray[i];
        [UIView animateWithDuration:0.3 delay:0.1 * i  usingSpringWithDamping:0.7 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
            btn.center = center;
            btn.transform = transform;
            btn.alpha = 0;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.6 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                if (i == _btnArray.count - 1) {
                    [self removeFromSuperview];
                    if (isClickBtn) {
                        if (self.shareBlock) {
                            self.shareBlock(btntag - buttonTag);
                        }
                    }
                }
              
            }];
            
        }];
    }
    
}
@end
